home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Disc to the Future 2
/
Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin
/
MAC
/
MACSHELL
/
MS1
/
COMMANDS
/
CRYPT.C
< prev
next >
Wrap
Text File
|
1992-12-02
|
11KB
|
502 lines
/*
* MacShell Source File
*
* Copyright (c) 1989, 1990, 1991, 1992 Suick Bay Technologies. All rights reserved.
*
*
* RESTRICTIONS ON MacShell program and source code.
*
* Ñ╩MacShell¬ is a product of Suick Bay Technologies and is provided for
* restricted use by the owner of the CDROM "Disk to the future II".
*
* Ñ╩No permission is granted for any commercial use without the written
* consent of the Suick Bay Technologies.
*
* Ñ╩No permission is granted for any redistribution of any kind use without
* the written consent of the Suick Bay Technologies.
*
* Ñ╩Permission is granted to use this for any personal noncommercial use.
*
* Ñ╩You may not distribute source or executable code at all, nor may you
* distribute it with or within a commercial product without the written
* consent of the Suick Bay Technologies. Please send modifications to
* the author for inclusion in updates to the program. Thanks.
*
*
* MacShell¬ IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
* WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
* PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
*
* SUICK BAY TECHNOLOGIES SHALL HAVE NO LIABILITY WITH RESPECT TO THE
* INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY MACSHELL
* OR ANY PART THEREOF.
*
* In no event will Suick Bay Technologies be liable for any lost revenue
* or profits or other special, indirect and consequential damages, even if
* Suick Bay Technologies has been advised of the possibility of such damages.
*
* Suick Bay Technologies can be reached at:
*
* 8768 Cottonwood lane
* Maple Grove, MN 55369
* Voice: (612) 425-7025
* AppleLink: D5233
*
*
* No parts of this software may be reproduced or stored in a
* retrieval system or transmitted in any form, or any means,
* electronic, mechanical, photocopying, recording or otherwise,
* without the prior written permission of Suick Bay Technologies.
*
* Spread the word and not the disk.
*
* SPK 012290 : Initial
*/
#include "SystemPub.h"
#include "Proc.h"
#include "ShellPub.h"
#include "Path.h"
#define CryptAbort (**MyShell).Proc[ProcID].bflags.f0
#define ROTORSIZE 256
#define MASK 0xFF
#define EMPTY (unsigned char)0xFF
#define BUFSIZE 256L
#define X_SIZE 256
unsigned char x[ X_SIZE ];
unsigned char rotor1[ ROTORSIZE ];
unsigned char rotor2[ ROTORSIZE ];
unsigned char rotor3[ ROTORSIZE ];
/********************************************************************/
void InvertRotor( unsigned char *r )
{
unsigned char t[ ROTORSIZE ];
int16 i;
for ( i = 0; i < ROTORSIZE; i++ )
t[ i ] = r[ i ];
for ( i = 0; i < ROTORSIZE; i++ )
r[ t[ i ] ] = i;
}
/********************************************************************/
void BeatIt( char *target, char *eggs, int16 end )
{
char *cp1, *cp2;
int16 len = 0;
cp1 = target;
while( *cp1 && len < end )
{
cp2 = eggs;
while( *cp2 )
{
*cp1 += *cp2;
cp2++;
}
len++;
cp1++;
}
while( len++ < end )
*cp1++ = *cp2 + 1;
}
/********************************************************************/
void InitCrypt( char *password, int16 decrypt )
{
int16 index;
unsigned i;
unsigned random;
int32 seed = 123L;
char buf[14];
char key[9];
char salt[3];
password[ 13 ] = '\0';
for( i = 0; i < 14; i++ )
buf[ i ] = '\0';
strcpy( buf, password );
salt[0] = buf[0];
salt[1] = buf[1];
salt[2] = '\0';
BeatIt( buf, salt, 13 );
for( i = 0 ; i < ROTORSIZE; i++ )
rotor1[i] = rotor2[i] = rotor3[i] = EMPTY;
for( i = 2; i < 13; i++ )
seed = seed * buf[i] + i;
i = 0;
while( i < ROTORSIZE )
{
seed = (int32)(5L * seed + (int32)i);
random = (unsigned)( seed % 65521L);
index = (int16)(random & MASK);
if (rotor1[index] == EMPTY)
rotor1[index] = ( unsigned char ) i++;
else
continue;
}
i = 0;
while( i < ROTORSIZE )
{
seed = (int32)(5L * seed + (int32)i);
random = (unsigned)( seed % 65521L);
index = (int16)(random & MASK);
if (rotor2[index] == EMPTY)
rotor2[index] = ( unsigned char ) i++;
else
continue;
}
i = 0;
while( i < ROTORSIZE )
{
seed = (int32)(5L * seed + (int32)i);
random = (unsigned)( seed % 65521L);
index = (int16)(random & MASK);
if (rotor3[index] == EMPTY)
rotor3[index] = ( unsigned char ) i++;
else
continue;
}
for( i = 0; i < X_SIZE; i++ )
{
seed = (int32)(5L * seed + (int32)i);
random = (unsigned)(seed % 65521L);
x[i] = random & 3;
}
if( decrypt )
{
InvertRotor( rotor1 );
InvertRotor( rotor2 );
InvertRotor( rotor3 );
}
}
/********************************************************************/
void EncryptFile( WHandle ShellWh, int16 ProcID, int16 srcRefNum,
int16 dstRefNum )
{
int16 i = 0;
unsigned char *ch, *cp, inBuf[ BUFSIZE ], outBuf[ BUFSIZE ];
unsigned char ofs1 = 0, ofs2 = 0, ofs3 = 0;
OSErr err = noErr;
int32 count, index;
ShellWindRec **MyShell = (ShellWindRec **) (**ShellWh).thing;
while( (err == noErr) && !CryptAbort )
{
if( UserAbort() )
CryptAbort = TRUE;
count = BUFSIZE;
err = FSRead( srcRefNum, &count, inBuf );
ch = inBuf;
cp = outBuf;
index = count;
for( index = 0; index < count; index++ )
{
*cp++ = (char)
rotor3[ (unsigned)
(rotor2[ (unsigned)
(rotor1[ ((unsigned) *ch + ofs1) & MASK ]
+ofs2) & MASK ])
+ofs3 & MASK ];
switch( x[i] )
{
case 0: ofs1 = ++ofs1 & MASK; break;
case 1: ofs2 = ++ofs2 & MASK; break;
case 2: ofs3 = ++ofs3 & MASK; break;
}
if( ofs1 == 0 )
ofs2 = ++ofs2 & MASK;
if (ofs2 == 0)
ofs3 = ++ofs3 & MASK;
if( ++i == X_SIZE )
i = 0;
ch++;
}
FSWrite( dstRefNum, &count, outBuf );
}
}
/********************************************************************/
void DecryptFile( WHandle ShellWh, int16 ProcID, int16 srcRefNum,
int16 dstRefNum )
{
int16 i = 0;
unsigned char *ch, *cp, inBuf[ BUFSIZE ], outBuf[ BUFSIZE ];
unsigned char ofs1 = 0, ofs2 = 0, ofs3 = 0;
OSErr err = noErr;
int32 count, index;
ShellWindRec **MyShell = (ShellWindRec **) (**ShellWh).thing;
while( (err == noErr) && !CryptAbort )
{
if( UserAbort() )
CryptAbort = TRUE;
count = BUFSIZE;
err = FSRead( srcRefNum, &count, inBuf );
ch = inBuf;
cp = outBuf;
index = count;
for( index = 0; index < count; index++ )
{
*cp++ = (char)
(rotor1[ (unsigned)
(rotor2[ (unsigned)
(rotor3[ (unsigned) (*ch) ] - ofs3) & MASK
] - ofs2) & MASK
] - ofs1) & MASK;
switch( x[ i ] )
{
case 0: ofs1 = ++ofs1 & MASK; break;
case 1: ofs2 = ++ofs2 & MASK; break;
case 2: ofs3 = ++ofs3 & MASK; break;
}
if( ofs1 == 0 )
ofs2 = ++ofs2 & MASK;
if (ofs2 == 0)
ofs3 = ++ofs3 & MASK;
if( ++i == X_SIZE )
i = 0;
ch++;
}
FSWrite( dstRefNum, &count, outBuf );
}
}
/*******************************************************************
*
* Function Crypt
*
* PathName Callback function
*
* usage Crypt [options] [names]
*
*
*******************************************************************/
static Boolean encrypt, fixSuppress, commandPW;
void CryptCallBack( WHandle ShellWh, int16 ProcID, char *path,
char *last, pathType what, int16 vRefNum, int32 dirID )
{
int16 srcRefNum, dstRefNum, tempDst = FALSE;
char buf[ 64 ];
OSErr err;
if( what == pathIsFile )
{
if( encrypt )
{
srcRefNum = OpenFileDirect( last, 'TEXT', fsRdPerm );
if( srcRefNum )
{
sprintf( buf, "%s.crypt", last );
CreateFile( buf, 'TEXT' );
dstRefNum = OpenFileDirect( buf, 'TEXT', fsWrPerm );
if( dstRefNum )
{
EncryptFile( ShellWh, ProcID, srcRefNum, dstRefNum );
FSClose( dstRefNum );
}
FSClose( srcRefNum );
}
}
else
{
sprintf( buf, "%s.crypt", last );
srcRefNum = OpenFileDirect( buf, 'TEXT', fsRdPerm );
if( srcRefNum == 0 )
srcRefNum = OpenFileDirect( last, 'TEXT', fsRdPerm );
if( srcRefNum )
{
sprintf( buf, "%s.decrypt", last );
CreateFile( buf, 'TEXT' );
dstRefNum = OpenFileDirect( buf, 'TEXT', fsWrPerm );
if( dstRefNum )
{
DecryptFile( ShellWh, ProcID, srcRefNum, dstRefNum );
FSClose( dstRefNum );
}
FSClose( srcRefNum );
}
}
if( fixSuppress )
{
char buf2[ 64 ];
/* copy the file back to the original name */
/* rename the original file */
strcpy( buf, last );
CtoPstr( buf );
err = HRename( vRefNum, dirID, buf, "\p____temp____" );
if( !err )
{
/* rename the encrypted file */
if( encrypt )
sprintf( buf2, "%s.crypt", last );
else
sprintf( buf2, "%s.decrypt", last );
CtoPstr( buf2 );
err = HRename( vRefNum, dirID, buf2, buf );
}
/* delete the renamed original file */
if( !err )
err = HDelete( vRefNum, dirID, "\p____temp____" );
}
}
}
/*******************************************************************/
void DoCryptFile( WHandle ShellWh, int16 ProcID, char *argument )
{
ShellWindRec **MyShell;
MyShell = (ShellWindRec **) (**ShellWh).thing;
ExpandPath( ShellWh, ProcID, argument, (ProcPtr) CryptCallBack,
(**MyShell).pwdVRefNum, (**MyShell).pwdDirID );
ResetShellPWD( ShellWh );
}
/*******************************************************************/
Boolean DoCRYPT( int16 ProcToken, WHandle ShellWh, int16 ProcID,
char *string )
{
int16 i, argc;
char *cp, argument[ 256 ];
ShellWindRec **MyShell = (ShellWindRec **) (**ShellWh).thing;
switch( ProcToken )
{
case PROC_INIT :
(**MyShell).Proc[ ProcID ].flags = TRUE;
break;
case PROC_TERM :
case PROC_BREAK :
CryptAbort = TRUE;
/* Tell the shell that we're done */
SendOutToken( ShellWh, ProcID, PROC_BREAK );
/* Turn ourself off */
(**MyShell).Proc[ ProcID ].ProcActive = FALSE;
break;
case PROC_STDIN :
if( (**MyShell).Proc[ ProcID ].flags )
{
(**MyShell).Proc[ ProcID ].flags = FALSE;
/* get arguments */
argc = (**MyShell).Proc[ ProcID ].argc;
GetArgv( ShellWh, ProcID, 0, argument );
encrypt = strcmp( "crypt", argument ) == 0;
fixSuppress = FALSE;
commandPW = FALSE;
CryptAbort = FALSE;
for( i = 1; i < argc; i++ )
{
GetArgv( ShellWh, ProcID, i, argument );
cp = argument;
if( *cp++ == '-' )
while( *cp && cp )
switch( *cp++ )
{
case 'p' : /* password */
{
char pw[ 256 ];
GetArgv( ShellWh, ProcID, ++i, pw );
InitCrypt( pw, !encrypt );
commandPW = TRUE;
cp = NULL;
}
break;
case 's' : /* suppress */
fixSuppress = TRUE;
break;
}
}
if( !commandPW )
{
char pw[ 256 ];
if( DialogAsk( "Your Password", pw ) )
{
InitCrypt( pw, !encrypt );
commandPW = TRUE;
}
}
if( commandPW )
for( i = 1; i < argc; i++ )
{
GetArgv( ShellWh, ProcID, i, argument );
if( *argument != '-' )
DoCryptFile( ShellWh, ProcID, argument );
else if( argument[ 1 ] == 'p' )
i++;
}
/* Tell the shell that we're done */
SendOutToken( ShellWh, ProcID, PROC_BREAK );
/* Turn ourself off */
(**MyShell).Proc[ ProcID ].ProcActive = FALSE;
return( FALSE );
}
}
}